﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebEntities;
using WebEntities.Models.WebInfo;

namespace com.subb.WebInformation
{
    public class WI_NewsCataDAL
    {
        public List<WI_NewsCata> GetAllNewsCata(int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.WI_NewsCata.OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.CataID);
                ttlCount = query.Count();
                List<WI_NewsCata> catalist = query.Skip(start).Take(limit).ToList();
                return catalist;
            }
        }

        public List<WI_NewsCata> GetAllNewsCata()
        {
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.WI_NewsCata.OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.CataID).AsQueryable();
                List<WI_NewsCata> catalist = query.ToList();
                return catalist;
            }
        }

        public List<NewsCata> GetAllUINewsCata(int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                var query = ctx.WI_NewsCata.OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.CataID);
                ttlCount = query.Count();
                List<WI_NewsCata> dbcatalist = query.Skip(start).Take(limit).ToList();
                List<NewsCata> catalist = new List<NewsCata>();
                foreach (var dbcata in dbcatalist)
                {
                    string rootCataName = string.Empty;
                    string parentCataName = string.Empty;
                    if (dbcata.WI_RootCata != null)
                    {
                        rootCataName = dbcata.WI_RootCata.RootCataName;
                    }
                    if (dbcata.ParentCataID.HasValue && dbcata.ParentCataID.Value > 0)
                    {
                        WI_NewsCata pncata = GetNewsCata(dbcata.ParentCataID.Value);
                        if (pncata != null)
                        {
                            parentCataName = pncata.CataName;
                        }
                    }
                    NewsCata ncata = NewsCata.AsNewsCata(dbcata, rootCataName, parentCataName);
                    catalist.Add(ncata);
                }
                return catalist;
            }
        }

        /// <summary>
        /// 按上级CataID获取新闻目录（预编译版本）。
        /// </summary>
        public List<WI_NewsCata> GetAllNewsCataByPcataId(int? pcataId, int start, int limit)
        {
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = NewsQuery.GetAllNewsCataQueryByPcata(ctx, pcataId, start, limit);
                List<WI_NewsCata> catalist = query.ToList();
                return catalist;
            }
        }

        public WI_NewsCata GetNewsCata(int cataId)
        {
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                WI_NewsCata ncata = ctx.WI_NewsCata.SingleOrDefault(c => c.CataID == cataId);
                return ncata;
            }
        }

        public void CreateNewsCata(NewsCata v_newscata)
        {
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                WI_NewsCata wicata = new WI_NewsCata
                {
                    CataName = v_newscata.CataName,
                    RootCataID = v_newscata.RootCataID,
                    SortNum = v_newscata.SortNum,
                    ParentCataID = v_newscata.ParentCataID,
                    Description = v_newscata.Description,
                    CataStatus = v_newscata.CataStatus,
                    CreateTime = DateTime.Now,
                    Creator = v_newscata.Creator,
                    CataCover = v_newscata.CataCover,
                    MenuItemID = v_newscata.MenuItemID
                };
                ctx.WI_NewsCata.AddObject(wicata);
                ctx.SaveChanges();
            }
        }

        public bool UpdateNewsCata(int cataId, NewsCata v_newscata)
        {
            bool result = false;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                WI_NewsCata ncata = ctx.WI_NewsCata.SingleOrDefault(c => c.CataID == cataId);
                if (ncata != null)
                {
                    ncata.CataName = v_newscata.CataName;
                    ncata.RootCataID = v_newscata.RootCataID;
                    ncata.SortNum = v_newscata.SortNum;
                    ncata.ParentCataID = v_newscata.ParentCataID;
                    ncata.Description = v_newscata.Description;
                    ncata.CataStatus = v_newscata.CataStatus;
                    ncata.CataCover = v_newscata.CataCover;
                    ncata.MenuItemID = v_newscata.MenuItemID;
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public bool RemoveNewsCata(int cataId)
        {
            bool result = false;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                WI_NewsCata ncata = ctx.WI_NewsCata.SingleOrDefault(c => c.CataID == cataId);
                if (ncata != null)
                {
                    //子目录的ParentCataID设为Null
                    List<WI_NewsCata> subCataList = ctx.WI_NewsCata.Where(c => c.ParentCataID == cataId).ToList();
                    foreach (var subcata in subCataList)
                    {
                        subcata.ParentCataID = null;
                    }
                    ctx.WI_NewsCata.DeleteObject(ncata);
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }
    }
}
